import requests
import random
from DataTest.JsonData.read_json import load_json
import json
import time
from DataTest.sql.data_sql import sqlCheck

suit = {'休闲之家': 'xiuxianzhijia', '收藏家': 'shoucangjia',
        '美食小屋': 'meishixiaowu', '魔法小屋': 'mofaxiaowu'}

furniture = {'巫师墙面': 'wushiqiangmian', '蛛丝地毯': 'zhusiditan',
             '蜘蛛网': 'zhizhuwang', '小猫布偶': 'xiaomaobuou',
             '巫师沙发': 'wushishafa', '巫师南瓜汤': 'wushinanguatang'}

buy_type = ['y', 'n']

path = '../JsonData/is_match_user.json'


def load_userid():
    self_id = list()

    with open(path, 'r', encoding='utf-8') as file:
        data = json.load(file)

        for item in data.get('data'):
            id = item.get('self_data').get('id')
            self_id.append(id)
        # print(self_id)
        return self_id


def buy_suit():
    """
    self_token钻石随机购买套装
    match_token贝壳随机购买家具
    :return:
    """
    # 为self_id添加贝壳和钻石
    id_list = load_userid()

    for user_id in id_list:
        sql = f'UPDATE matches SET shells=1000,diamonds=1000 WHERE user_id1={user_id} or user_id2={user_id};'
        sqlCheck(sql, 'suki')

    time.sleep(1)
    # 为self_token购买套装
    self_token_list, match_token_list = load_json('../JsonData/is_match_user.json')
    goods_buy_url = 'http://124.220.33.63:8700/goods/buy'

    for token in self_token_list:

        header = {
            'Authorization': f'Bearer {token}',
            'Content-Type': 'application/json',
        }
        _buy = random.choice(buy_type)

        if _buy == 'y':
            key = random.choice(list(suit))

            values = suit[key]

            data = {
                "Keys": [
                ],
                "DiamondKeys": [
                    values
                ]
            }

            res = requests.post(url=goods_buy_url, headers=header, json=data)

            if res.status_code == 200:
                print(f'用户{token}购买套装{key}成功')

            elif res.status_code == 1002:
                print(f'已购买套装{key}')

            else:
                print(f'用户{token}购买套装{key}失败或钻石不足')

        elif _buy == 'n':
            print(f'用户{token}不购买套装')

    for token in match_token_list:

        header = {
            'Authorization': f'Bearer {token}',
            'Content-Type': 'application/json',
        }

        _buy = random.choice(buy_type)

        if _buy == 'y':
            key = random.choice(list(furniture))
            values = furniture[key]

            data = {
                "Keys": [
                    values
                ],
                "DiamondKeys": [
                ]
            }

            res = requests.post(url=goods_buy_url, headers=header, json=data)

            if res.status_code == 200:
                print(f'用户{token}购买家具{key}成功')

            elif res.status_code == 1002:
                print(f'已购买家具{key}')

            else:
                print(f'用户{token}购买家具{key}失败或贝壳不足')

        elif _buy == 'n':
            print(f'用户{token}不购买家具')


if __name__ == '__main__':
    # load_userid()
    buy_suit()
